package com.wang.cloud.common.mysql.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * 自动填充时间
 *
 * @author wangye
 * @date 2023/7/14 0014
 */
public class MyMetaObjectHandler implements MetaObjectHandler {
    private String getUserIdStr() {
        try {
// TODO 暂时-定义操作用户为null
//            Long userId = SecurityUtils.getUserId();
//            String userIdStr = ObjectUtil.isNotEmpty(userId) ? userId.toString() : "";
//            return userIdStr;
            return "";
        } catch (Exception e) {
            return "";
        }
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        String userIdStr = getUserIdStr();
        LocalDateTime now = LocalDateTime.now();
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, now);
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, now);
        this.strictInsertFill(metaObject, "createBy", String.class, userIdStr);
        this.strictInsertFill(metaObject, "updateBy", String.class, userIdStr);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String userIdStr = getUserIdStr();
        LocalDateTime now = LocalDateTime.now();
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, now);
        this.strictInsertFill(metaObject, "updateBy", String.class, userIdStr);
    }
}
